<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>产品标签打印系统</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 800px;
            margin: 0 auto;
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        .required {
            color: #dc3545;
        }
        input[type="text"], input[type="number"], textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            box-sizing: border-box;
        }
        textarea {
            height: 80px;
            resize: vertical;
        }
        input[type="text"]:focus, input[type="number"]:focus, textarea:focus {
            border-color: #007bff;
            outline: none;
            box-shadow: 0 0 5px rgba(0,123,255,0.3);
        }
        .btn {
            background-color: #007bff;
            color: white;
            padding: 12px 30px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            width: 100%;
            margin-top: 10px;
        }
        .btn:hover {
            background-color: #0056b3;
        }
        .btn:disabled {
            background-color: #6c757d;
            cursor: not-allowed;
        }
        .btn-secondary {
            background-color: #6c757d;
            margin-right: 10px;
            width: auto;
        }
        .btn-secondary:hover {
            background-color: #545b62;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
            display: none;
        }
        .result.success {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .result.error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .loading {
            display: none;
            text-align: center;
            margin-top: 10px;
        }
        .spinner {
            border: 4px solid #f3f3f3;
            border-top: 4px solid #007bff;
            border-radius: 50%;
            width: 30px;
            height: 30px;
            animation: spin 1s linear infinite;
            margin: 0 auto;
        }
        @keyframes spin {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        .form-row {
            display: flex;
            gap: 20px;
        }
        .form-row .form-group {
            flex: 1;
        }
        .navigation {
            text-align: center;
            margin-bottom: 20px;
        }
        .navigation a {
            color: #007bff;
            text-decoration: none;
            margin: 0 10px;
        }
        .navigation a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="navigation" style="text-align: left;">
            <a href="/" style="font-size: 14px;">← 返回主菜单</a>
        </div>
        
        <h1>产品标签打印系统</h1>
        <form id="productPrintForm">
            <div class="form-group">
                <label for="productName">产品名称 <span class="required">*</span>:</label>
                <textarea id="productName" name="productName" required placeholder="请输入产品名称（支持长文本自动折行）"></textarea>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label for="manufacturer">生产商 <span class="required">*</span>:</label>
                    <input type="text" id="manufacturer" name="manufacturer" required placeholder="请输入生产商">
                </div>
                
                <div class="form-group">
                    <label for="licenseNumber">生产许可证编号:</label>
                    <input type="text" id="licenseNumber" name="licenseNumber" placeholder="请输入生产许可证编号">
                </div>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label for="postalCode">邮编:</label>
                    <input type="text" id="postalCode" name="postalCode" placeholder="请输入邮编">
                </div>
                
                <div class="form-group">
                    <label for="productionBatch">生产批次 <span class="required">*</span>:</label>
                    <input type="text" id="productionBatch" name="productionBatch" required placeholder="请输入生产批次">
                </div>
            </div>
            
            <div class="form-row">
                <div class="form-group">
                    <label for="bagCount">袋数 <span class="required">*</span>:</label>
                    <input type="number" id="bagCount" name="bagCount" min="1" required placeholder="请输入袋数">
                </div>
                
                <div class="form-group">
                    <label for="specification">规格 <span class="required">*</span>:</label>
                    <input type="text" id="specification" name="specification" required placeholder="请输入规格">
                </div>
            </div>
            
            <div style="text-align: center; margin-top: 30px;">
                <button type="button" class="btn btn-secondary" onclick="clearForm()">清空表单</button>
                <button type="submit" class="btn" id="printBtn">打印标签</button>
            </div>
        </form>
        
        <div class="loading" id="loading">
            <div class="spinner"></div>
            <p>正在打印中，请稍候...</p>
        </div>
        
        <div class="result" id="result">
            <div id="resultMessage"></div>
        </div>
    </div>

    <script>
        document.getElementById('productPrintForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const formData = new FormData(this);
            const printBtn = document.getElementById('printBtn');
            const loading = document.getElementById('loading');
            const result = document.getElementById('result');
            const resultMessage = document.getElementById('resultMessage');
            
            // 验证必填字段
            const productName = formData.get('productName').trim();
            const manufacturer = formData.get('manufacturer').trim();
            const productionBatch = formData.get('productionBatch').trim();
            const bagCount = formData.get('bagCount');
            const specification = formData.get('specification').trim();
            
            if (!productName) {
                showResult('error', '产品名称不能为空');
                return;
            }
            
            if (!manufacturer) {
                showResult('error', '生产商不能为空');
                return;
            }
            
            if (!productionBatch) {
                showResult('error', '生产批次不能为空');
                return;
            }
            
            if (!bagCount || parseInt(bagCount) < 1) {
                showResult('error', '袋数必须大于0');
                return;
            }
            
            if (!specification) {
                showResult('error', '规格不能为空');
                return;
            }
            
            // 显示加载状态
            printBtn.disabled = true;
            loading.style.display = 'block';
            result.style.display = 'none';
            
            // 发送打印请求
            fetch('/api/print-product', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    showResult('success', data.message);
                } else {
                    showResult('error', data.message);
                }
            })
            .catch(error => {
                console.error('Error:', error);
                showResult('error', '请求失败，请检查网络连接');
            })
            .finally(() => {
                printBtn.disabled = false;
                loading.style.display = 'none';
            });
        });
        
        function showResult(type, message) {
            const result = document.getElementById('result');
            const resultMessage = document.getElementById('resultMessage');
            
            result.className = 'result ' + type;
            resultMessage.innerHTML = message;
            result.style.display = 'block';
        }
        
        function clearForm() {
            document.getElementById('productPrintForm').reset();
            document.getElementById('result').style.display = 'none';
        }
    </script>
</body>
</html>